/* **************************************************************************************** */
/* TABLE 5: GROWTH OF EXPORTS DECOMPOSITION: EXTENSIVE, INTENSIVE (SURVIVAL AND DEEPENINNG) */
/* **************************************************************************************** */
/*Hazard rates vary by year, year of service and country*/
/*Counterfactual case: how exports' growth would vary if all hazard rates were calculated only with exports to EU15 countries*/
/*Only hazard rates vary. No change in deepening or entry*/

cd file_aux
drop _all
set mem 500000k

/*Sample SPANISH REGIONS */
/*Counterfactual country: AVERAGE SPANISH REGIONS */

local year_c "1988 1989 1990 1991 1992 1993 1994 1995 1996 1997 1998 1999 2000 2001 2002 2003 2004"

foreach yy of local year_c{

local cou_iso3 "AND ARA AST BAL CAN CAT CMA CLE CTN EXT GAL MAD MUR NAV PVS RIO VAL"

set more off

/*Calculate Equation 2's components*/
/*Calculate 3 countries' hazard rates (as in program_table3.do)*/

foreach zz of local cou_iso3 {
u cou_`zz', replace
keep if year==`yy'



/*drop if leftcensored==1*/
ge year0=year-1
stset year, f(failure) id(spell) origin(begin) time0(year0)

sts gen surv=s
label var surv "% surviving rel at time t"
sts gen haz=h
label var haz "exit risk conditionated to be until t"


sort expseq
keep if expseq!=expseq[_n-1]
keep reporter_iso3 expseq surv haz
replace surv=0 if surv==.
replace haz=0 if haz==.
sort reporter_iso3 expseq

save aux_table5_sh_c_`zz', replace
}

/*Calculate 3 countries' number of relationships per year*/
foreach zz of local cou_iso3 {
u cou_`zz', replace
keep if year==`yy'
egen n=count(value), by(year expseq)
keep reporter_iso3 year expseq n
duplicates drop reporter_iso3 year expseq, force
sort reporter_iso3 expseq
save aux_table5_n_c_`zz', replace
}

/*Calculate 3 countries' exports' average value per year*/
foreach zz of local cou_iso3 {
u cou_`zz', replace
keep if year==`yy'
egen v=mean(value), by(year expseq)
keep reporter_iso3 year expseq v
duplicates drop reporter_iso3 year expseq, force
sort reporter_iso3 year expseq
save aux_table5_v_c_`zz', replace
}

/*Calculate deepening rates*/
foreach zz of local cou_iso3{
u cou_`zz'
scalar k=`yy'+1
drop if year>k
drop if year<=`yy'
keep if expseq>1


egen v2=mean(value), by(year expseq)
keep reporter_iso3 year expseq v2
duplicates drop year expseq, force
replace expseq=expseq-1
replace year=year-1
sort reporter_iso3 year expseq

merge reporter_iso3 year expseq using aux_table5_v_c_`zz'
keep if _merge==3
drop _merge
ge d=v2-v
drop v
sort reporter_iso3 year expseq
save aux_table5_d_c_`zz', replace
}

/*Calculate 3 countries' total exports at year t*/
foreach zz of local cou_iso3 {
u cou_`zz', replace
keep if year==`yy'
egen tx_t=total(value), by(year)
keep reporter_iso3 year expseq tx_t
duplicates drop reporter_iso3 year, force
sort reporter_iso3 year
save aux_table5_txt_c_`zz', replace
}



/*Calculate 3 countries' total exports at year t+1*/
foreach zz of local cou_iso3 {
u cou_`zz', replace
drop if year>k
egen tx_t1=total(value), by(year)
drop if year<=`yy'
replace year=year-1
duplicates drop year, force
keep reporter_iso3 year tx_t1
sort reporter_iso3 year
save aux_table5_txt_c1_`zz', replace
}






/*Calculate 3 countries' entry rate and new entries' value*/
foreach zz of local cou_iso3 {
u cou_`zz', replace
drop if year>k
drop if year<=`yy'
keep if expseq==1
egen e=count(value), by(year)
egen ve=mean(value), by (year)
keep reporter_iso3 year e ve
duplicates drop reporter_iso3 year, force
replace year=year-1
sort reporter_iso3 year
save aux_table5_e_c_`zz', replace
}


/*Construction of the matrix for the counterfactual calculations*/
foreach zz of local cou_iso3{
use aux_table5_n_`zz'
sort reporter_iso3 expseq
merge reporter_iso3 expseq using aux_table5_sh_c_`zz'
keep if _merge==3
drop _merge

sort reporter_iso3 year expseq
merge reporter_iso3 year expseq using aux_table5_d_c_`zz'
drop _merge

sort reporter_iso3 year expseq
merge reporter_iso3 year expseq using aux_table5_v_c_`zz'
drop _merge

sort reporter_iso3 year expseq
merge reporter_iso3 year using aux_table5_txt_c_`zz'
drop _merge


sort reporter_iso3 year 
merge reporter_iso3 year using aux_table5_txt_c1_`zz'
drop _merge


sort reporter_iso3 year
merge reporter_iso3 year using aux_table5_e_c_`zz'
drop _merge

drop if year==2006 		 /* CHOOSE LAST YEAR OF THE ANALYSED PERIOD */
save aux_table5_c_`zz', replace	/* **************************************** */
}


/*Calculation of the counterfactual hazard rates*/
****************************************************
use cou_AND

local cou_iso3a "ARA AST BAL CAN CAT CMA CLE CTN EXT GAL MAD MUR NAV PVS RIO VAL"

foreach zz of local cou_iso3a{
append using cou_`zz'
}

keep if year==`yy'

/* EU15 partner countries*/
keep if partner_iso3=="AUT" | partner_iso3=="BEL" | partner_iso3=="DNK" | partner_iso3=="FIN" | partner_iso3=="FRA" | partner_iso3=="DEU" | partner_iso3=="GRC" | partner_iso3=="IRL" | partner_iso3=="ITA" | partner_iso3=="LUX" | partner_iso3=="NLD" | partner_iso3=="PRT" | partner_iso3=="SWE" | partner_iso3=="GBR"


/*drop if leftcensored==1*/
ge year0=year-1

stset year, f(failure) id(spell) origin(begin) time0(year0)

sts gen haz=h, by(reporter_iso3)
label var haz "exit risk conditionated to be until t"

duplicates drop reporter_iso3 expseq, force
keep reporter_iso3 expseq haz
replace haz=0 if haz==.
sort reporter_iso3 expseq

save country_h, replace



/*Counterfactual deepening rates*/
use cou_AND

local cou_iso3a "ARA AST BAL CAN CAT CMA CLE CTN EXT GAL MAD MUR NAV PVS RIO VAL"

foreach zz of local cou_iso3a{
append using cou_`zz'
}

keep if year==`yy'

/* EU15 partner countries*/
keep if partner_iso3=="AUT" | partner_iso3=="BEL" | partner_iso3=="DNK" | partner_iso3=="FIN" | partner_iso3=="FRA" | partner_iso3=="DEU" | partner_iso3=="GRC" | partner_iso3=="IRL" | partner_iso3=="ITA" | partner_iso3=="LUX" | partner_iso3=="NLD" | partner_iso3=="PRT" | partner_iso3=="SWE" | partner_iso3=="GBR"

egen v=mean(value), by(year reporter_iso3 expseq)
keep year reporter_iso3 expseq v
duplicates drop year reporter_iso3 expseq, force
sort year reporter_iso3 expseq
save country_v, replace



/*Calculate deepening rates*/
use cou_AND
foreach zz of local cou_iso3a{
append using cou_`zz'
}

keep if partner_iso3=="AUT" | partner_iso3=="BEL" | partner_iso3=="DNK" | partner_iso3=="FIN" | partner_iso3=="FRA" | partner_iso3=="DEU" | partner_iso3=="GRC" | partner_iso3=="IRL" | partner_iso3=="ITA" | partner_iso3=="LUX" | partner_iso3=="NLD" | partner_iso3=="PRT" | partner_iso3=="SWE" | partner_iso3=="GBR"

scalar k=`yy'+1
drop if year>k
drop if year<=`yy'
keep if expseq>1

egen v2=mean(value), by(year reporter_iso3 expseq)
keep year reporter_iso3 expseq v2
duplicates drop year reporter_iso3 expseq, force
replace expseq=expseq-1
replace year=year-1
sort year reporter_iso3 expseq

merge year reporter_iso3 expseq using country_v
keep if _merge==3
drop _merge
ge d=v2-v
drop v
sort year reporter_iso3 expseq
save country_d, replace



/*Counterfactual entry rates*/
use cou_AND
foreach zz of local cou_iso3a{
append using cou_`zz'
}

keep if partner_iso3=="AUT" | partner_iso3=="BEL" | partner_iso3=="DNK" | partner_iso3=="FIN" | partner_iso3=="FRA" | partner_iso3=="DEU" | partner_iso3=="GRC" | partner_iso3=="IRL" | partner_iso3=="ITA" | partner_iso3=="LUX" | partner_iso3=="NLD" | partner_iso3=="PRT" | partner_iso3=="SWE" | partner_iso3=="GBR"


drop if year>k
drop if year<=`yy'
keep if expseq==1
egen e=count(value), by(year reporter_iso3)
egen ve=mean(value), by (year reporter_iso3)
keep year reporter_iso3 e ve
duplicates drop year reporter_iso3, force
replace year=year-1
sort year reporter_iso3
save country_e, replace



/* ***************************************************** */
/* ***************************************************** */
/* Using the counterfactual data 				   */
/* Counterfactual = EU15 countries' hazard rates 		   */
/* ***************************************************** */
/* ***************************************************** */

drop _all
local cou_rest "ARA AST BAL CAN CAT CMA CLE CTN EXT GAL MAD MUR NAV PVS RIO VAL"
use aux_table5_c_AND
foreach zz of local cou_rest{
append using aux_table5_c_`zz'
}

rename haz haz_country
rename d d_country
rename e e_country

sort reporter_iso3 expseq
merge reporter_iso3 expseq using country_h
ge haz_counter=.
replace haz_counter=haz if _merge==3
replace haz_counter=haz_country if _merge==1

drop if _merge==2
drop _merge


sort year reporter_iso3 expseq
merge year reporter_iso3 expseq using country_d
ge d_counter=.
replace d_counter=d if _merge==3
replace d_counter=d_country if _merge==1
drop d
drop if _merge==2
drop _merge


sort year reporter_iso3
rename ve ve_country
merge year reporter_iso3 using country_e
drop if _merge==2
ge e_counter=.
replace e_counter=e if _merge==3
replace e_counter=e_country if _merge==1
drop e ve
rename ve_country ve
drop _merge



replace haz_country=0 if haz_country==.
replace haz_counter=0 if haz_counter==.
replace n=0 if n==.
replace d_country=0 if d_country==.
replace d_counter=0 if d_counter==.
replace e_country=0 if e_country==.
replace e_counter=0 if e_counter==.
replace v=0 if v==.
replace ve=0 if ve==.
replace tx_t=0 if tx_t==.
replace tx_t1=0 if tx_t1==.


ge intensive0=((1-haz_country)*n*d_country)-(haz_country*n*v)
egen intensive1=total(intensive0), by(reporter_iso3 year)

ge h_intensive0=((1-haz_counter)*n*d_country)-(haz_counter*n*v)
egen h_intensive1=total(h_intensive0), by(reporter_iso3 year)

ge d_intensive0=((1-haz_country)*n*d_counter)-(haz_country*n*v)
egen d_intensive1=total(d_intensive0), by(reporter_iso3 year)


save en_curso, replace

duplicates drop reporter_iso3 year, force


ge chi=h_intensive1+(e_country*ve)
ge cdi=d_intensive1+(e_country*ve)
ge cei=intensive1+(e_counter*ve)
ge actual_increase=tx_t1-tx_t
ge actual_increase2=intensive1+(e_country*ve)


keep reporter_iso3 year chi cdi cei actual_increase actual_increase2 tx_t

save en_curso_`yy', replace
}


local year_c0 "1989 1990 1991 1992 1993 1994 1995 1996 1997 1998 1999 2000 2001 2002 2003 2004"
use en_curso_1988
foreach yy of local year_c0{
append using en_curso_`yy'
}



egen t_chi=total(chi), by(reporter_iso3)
egen t_cdi=total(cdi), by(reporter_iso3)
egen t_cei=total(cei), by(reporter_iso3)
egen t_actual_increase=total(actual_increase), by(reporter_iso3)
egen t_actual_increase2=total(actual_increase2), by (reporter_iso3)

save en_curso, replace



keep if year==1988


ge a=tx_t+t_actual_increase
ge b=a
replace b=tx_t-t_actual_increase if a<0 
ge actual_a_a_g=((b/tx_t)^(1/17))-1
replace actual_a_a_g=(-1)*actual_a_a_g if a<0
drop a b

ge a=tx_t+t_chi
ge b=a
replace b=tx_t-t_chi if a<0 
ge c_h_a_a_g=((b/tx_t)^(1/17))-1
replace c_h_a_a_g=(-1)*c_h_a_a_g if a<0
drop a b

ge a=tx_t+t_cdi
ge b=a
replace b=tx_t-t_cdi if a<0 
ge c_d_a_a_g=((b/tx_t)^(1/17))-1
replace c_d_a_a_g=(-1)*c_d_a_a_g if a<0
drop a b


ge a=tx_t+t_cei
ge b=a
replace b=tx_t-t_cei if a<0 
ge c_e_a_a_g=((b/tx_t)^(1/17))-1
replace c_e_a_a_g=(-1)*c_e_a_a_g if a<0
drop a b



ge d_h=(c_h_a_a_g-actual_a_a_g)*100
ge d_d=(c_d_a_a_g-actual_a_a_g)*100
ge d_e=(c_e_a_a_g-actual_a_a_g)*100

replace actual_a_a_g=actual_a_a_g*100


keep reporter_iso3 actual_a_a_g d_h d_d d_e
sort reporter_iso3


cd ..
log using res_table5_c.text, text replace
/****************************************************************/
/*           Counterfactual Analysis                            */
l,clean
/****************************************************************/
log close


